﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.Data.SqlServerCe;

using RFMobile.Database;

namespace RFMobile.Session
{
    public sealed class mobileSession
    {
        private DatabaseManager dbManager;
        private SqlCeDataReader dr;

        // Session attributes from responsible table
        public Int32 comtec_id_user;
        public Int32 comtec_id_mainteiner;
        public Int32 access_level;
        public Int32 status_reg;
        public DateTime income_date;
        public DateTime birthdate;
        public String name;
        public String lastname1;
        public String lastname2;
        public String phone;
        public String email;
        public String position;
        private String pass;
        private String passwmd5;
        public String rut;
        public String notes;
        public String cod_mainteiner;
        public String company_income_date;
        public String tag_id;

        private string string2md5(string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        public String password
        {
            set
            {
                pass = value;
                passwmd5 = string2md5(value);
            }
        }

        public Boolean Login(String user, String passwd)
        {
            passwmd5 = string2md5(passwd);
            dbManager = new DatabaseManager();
            if (dbManager.Query("SELECT * FROM users WHERE email = '" + user + "'"))
            {
                dr = dbManager.Command.ExecuteReader();
                while (dr.Read())
                {
                    if (passwmd5.Equals(dr["passwmd5"].ToString()))
                    {
                        name = dr["name"].ToString();
                        lastname1 = dr["lastname1"].ToString();
                        lastname2 = dr["lastname2"].ToString();
                        phone = dr["phone"].ToString();
                        email = dr["email"].ToString();
                        position = dr["position"].ToString();
                        passwmd5 = dr["passwmd5"].ToString();
                        rut = dr["rut"].ToString();
                        notes = dr["notes"].ToString();
                        cod_mainteiner = dr["cod_mainteiner"].ToString();
                        company_income_date = dr["company_income_date"].ToString();
                        tag_id = dr["tag_id"].ToString();

                        income_date = Convert.ToDateTime(dr["income_date"]);
                        birthdate = Convert.ToDateTime(dr["birthdate"]);

                        comtec_id_user = Convert.ToInt32(dr["comtec_id_user"]);
                        comtec_id_mainteiner = Convert.ToInt32(dr["comtec_id_mainteiner"]);
                        access_level = Convert.ToInt32(dr["access_level"]);
                        status_reg = Convert.ToInt32(dr["status_reg"]);
                    }
                    else
                    {
                        dr.Close();
                        dr.Dispose();
                        dbManager.Close();
                        dbManager = null;
                        return (false);
                    }
                }
                dr.Close();
                dr.Dispose();
                dbManager.Close();
                dbManager = null;
                return (true);
            }
            else
            {
                dbManager.Close();
                dbManager = null;
                return (false);
            }
        }

        private void Clear()
        {
            name = "";
            lastname1 = "";
            lastname2 = "";
            phone = "";
            email = "";
            position = "";
            passwmd5 = "";
            rut = "";
            notes = "";
            cod_mainteiner = "";
            company_income_date = "";
            tag_id = "";

            income_date = new DateTime();
            birthdate = new DateTime();
            
            comtec_id_user = 0;
            comtec_id_mainteiner = 0;
            access_level = 0;
            status_reg = 0;
        }

        public void Logout()
        {
            Clear();
        }

        // singleton class

        static readonly mobileSession instance = new mobileSession();

        static mobileSession()
        {
        }

        mobileSession()
        {
            Clear();
        }

        public static mobileSession Instance
        {
            get
            {
                return instance;
            }
        }
    }
}
